package yzm_demo;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;


public class EncryptUtil {

    private static final String IV_DEFAULT = "aaaaaaaaaaaaaaaa";
    private static final String AES = "AES";
    private static final String CYPHER_MODE = "AES/CBC/NoPadding";
    private static final String CHARSET = "utf-8";
    private static final String ALGORITHM = "SHA1PRNG";

    public static EncryptUtil me;

    private EncryptUtil() {
        // 单例
    }

    // 双重锁
    public static EncryptUtil getInstance() {
        if (me == null) {
            synchronized (EncryptUtil.class) {
                if (me == null) {
                    me = new EncryptUtil();
                }
            }
        }
        return me;
    }

    /**
     * 使用AES加密算法经行加密（可逆）
     *
     * @param res
     *            需要加密的密文
     * @param key
     *            秘钥
     * @return
     */
    public String AESencode(String res, String key) {
        byte[] cyphertext;
        try {
            cyphertext = encrypt(key.getBytes(), IV_DEFAULT.getBytes(), res.getBytes(CHARSET));
            // return Base64.getEncoder().encodeToString(cyphertext);
            return parseByte2HexStr(cyphertext);
        } catch (Exception e) {
           // log.error("AESencode:", e);
        }
        return null;
    }

    /**
     * 对使用AES加密算法的密文进行解密
     *
     * @param res
     *            需要解密的密文
     * @param key
     *            秘钥
     * @return
     */
    public String AESdecode(String res, String key) {
        // byte[] de_b64 = decrypt(key.getBytes(), IV_DEFAULT.getBytes(),
        // Base64.getDecoder().decode(res));
        byte[] de_b64 = decrypt(key.getBytes(), IV_DEFAULT.getBytes(), parseHexStr2Byte(res));
        return new String(de_b64);
    }

    private byte[] encrypt(byte[] key, byte[] initVector, byte[] value) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector);

            byte[] enCodeFormat = genSha1prng(key);

            SecretKeySpec skeySpec = new SecretKeySpec(enCodeFormat, AES);

            Cipher cipher = Cipher.getInstance(CYPHER_MODE);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            int blockSize = cipher.getBlockSize();
            byte[] plaintext = padding(value, blockSize);
            return cipher.doFinal(plaintext);
        } catch (Exception ex) {
           // log.error("encrypt:", ex);
        }

        return null;
    }

    private byte[] decrypt(byte[] key, byte[] initVector, byte[] encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector);

            byte[] enCodeFormat = genSha1prng(key);

            SecretKeySpec skeySpec = new SecretKeySpec(enCodeFormat, AES);

            Cipher cipher = Cipher.getInstance(CYPHER_MODE);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            return unpadding(cipher.doFinal(encrypted));
        } catch (Exception ex) {
           // log.error("decrypt:", ex);
        }

        return null;
    }

    private byte[] genSha1prng(byte[] key) throws NoSuchAlgorithmException {
        KeyGenerator kgen = KeyGenerator.getInstance(AES);
        SecureRandom secureRandom = SecureRandom.getInstance(ALGORITHM);
        secureRandom.setSeed(key);
        kgen.init(128, secureRandom);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        return enCodeFormat;
    }

    private byte[] padding(byte[] value, int blockSize) {
        int plaintextLength = value.length;
        if (plaintextLength % blockSize != 0) {
            plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
        }
        byte[] plaintext = new byte[plaintextLength];
        System.arraycopy(value, 0, plaintext, 0, value.length);
        return plaintext;
    }

    private byte[] unpadding(byte[] bytes) {
        int i = bytes.length - 1;
        while (i >= 0 && bytes[i] == 0) {
            --i;
        }

        return Arrays.copyOf(bytes, i + 1);
    }

    /** 将16进制转换为二进制 */
    private byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    /** 将二进制转换成16进制 */
    private String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        EncryptUtil a = EncryptUtil.getInstance();
        String aeSencode = a.AESencode("123123", "key");
        System.out.println(aeSencode);
        System.out.println(a.AESdecode(aeSencode,"key"));
    }
}